#include "stm32f10x.h"                  // Device header
#include "OLED.h"
#include "PWM.h"
#include "Key.h"



uint8_t i=0;			//亮度梯度
uint8_t set_light_flag;//亮度改变标志位
void Show_Light_UI(void)
{
	OLED_ShowImage(0,0,16,16,Image_Return);
	if(i==0)OLED_ShowImage(48,16,32,32,Image_FlashLight0);
	else if(i==33)OLED_ShowImage(48,16,32,32,Image_FlashLight1);
	else if(i==66)OLED_ShowImage(48,16,32,32,Image_FlashLight2);
	else if(i==99)OLED_ShowImage(48,16,32,32,Image_FlashLight3);
	OLED_ShowString(0,48,"OFF",OLED_8X16);
	OLED_ShowImage(56,48,16,16,Image_Add);
	OLED_ShowString(112,48,"ON",OLED_8X16);
}


extern uint8_t KeyNum;
uint8_t flashlight_flag=1;
uint8_t FlashLight(void)
{
    PWM_Init();
    
    while(1)
    {
        KeyNum = Key_GetNum();
        
        // 处理按键
        if(KeyNum == 1)
        {
            flashlight_flag--;
            if(flashlight_flag < 1) flashlight_flag = 4;
        }
        else if(KeyNum == 2)
        {
            flashlight_flag++;
            if(flashlight_flag > 4) flashlight_flag = 1;
        }
        else if(KeyNum == 3)
        {
            // 确认键
            switch(flashlight_flag)
            {
                case 1: 
                    // 返回
                    OLED_Clear();
                    OLED_Update();
                    return 0;  // 直接退出
                case 2: 
                    i = 0;
                    set_light_flag = 0;  // 注意：这里我们直接设置亮度，所以不需要set_light_flag
                    break;
                case 3:
                    set_light_flag = 1;  // 标记亮度需要增加
                    break;
                case 4:
                    i = 99;  // 最大亮度
                    set_light_flag = 0;
                    break;
            }
        }
        
        // 处理亮度设置
        if(set_light_flag == 1)
        {
            i += 33;
            if(i >= 100) i = 0;
            set_light_flag = 0;  // 一次只增加一档
        }
        PWM_SetCompare1(i);
        
        // 显示更新
        OLED_Clear();
        Show_Light_UI();
        
        // 根据当前选项高亮
        switch(flashlight_flag)
        {
            case 1:
                OLED_ReverseArea(0,0,16,16);
                break;
            case 2:
                OLED_ReverseArea(0,48,24,16);
                break;
            case 3:
                OLED_ReverseArea(56,48,16,16);
                break;
            case 4:
                OLED_ReverseArea(112,48,16,16);
                break;
        }
        OLED_Update();
        
    }
}

